package cn.tedu.myweb.service.impl;

import cn.tedu.myweb.exception.ServiceException;
import cn.tedu.myweb.mapper.FriendsMapper;
import cn.tedu.myweb.mapper.RoomMapper;
import cn.tedu.myweb.mapper.UserMapper;
import cn.tedu.myweb.model.*;
import cn.tedu.myweb.service.IChatsService;
import cn.tedu.myweb.service.IFriendsService;
import cn.tedu.myweb.vo.FriendVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.ibatis.annotations.Select;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class FriendsServiceImpl extends ServiceImpl<FriendsMapper, Friends> implements IFriendsService {

    @Autowired
    UserMapper userMapper;

    @Autowired
    FriendsMapper friendsMapper;

    @Autowired
    RoomMapper roomMapper;

    @Override
    public List<Friends> getMyFriend(String username) {
        User user = userMapper.findUserByUsername(username);
        if(user==null){
            return null;
        }
        QueryWrapper<Friends> query = new QueryWrapper<>();
        query.eq("user_id",user.getId());
        List<Friends> list = friendsMapper.selectList(query);
        for(Friends f : list){
            List<Chats> chats = friendNameToChats(f.getFriendName(),f.getMasterId(),user.getId());
            f.setUserName(username);
            f.setChats(chats);
        }
        return list;
    }

    @Autowired
    IChatsService chatsService;

    @Override
    public List<Chats> friendNameToChats(String friendName, int master,int userId) {
        List<Chats> chats = chatsService.getChatList(friendName,master,userId);

        return chats;
    }

    @Override
    public void addRoom(String username, String friendname) {
        User user = userMapper.findUserByUsername(username);
        User friend = userMapper.findUserByUsername(friendname);
        Room room = new Room()
                .setUserId1(friend.getId())
                        .setUserId2(user.getId());
        int num=roomMapper.insert(room);
        if(num!=1){
            throw new ServiceException("数据库忙！");
        }
    }

    @Override
    public void addFriends(String friendName,String username) {
        User user = userMapper.findUserByUsername(username);
        User friend = userMapper.findUserByUsername(friendName);
        Friends friend1 = new Friends()
                .setRoomId(roomMapper.findRoomIdByUserId(user.getId()))
                .setFriendId(user.getId())
                .setFriendName(username)
                .setUserId(friend.getId())
                .setMasterId(friend.getId());
        Friends friend2 = new Friends()
                .setRoomId(roomMapper.findRoomIdByUserId(user.getId()))
                .setFriendId(friend.getId())
                .setFriendName(friendName)
                .setUserId(user.getId())
                .setMasterId(friend.getId());
        int num=friendsMapper.insert(friend1);
        int num2=friendsMapper.insert(friend2);
        if(num!=1 || num!=1){
            throw new ServiceException("数据库忙！");
        }



    }


}
